package com.woniu.bh.beautysalon.web.controller;


import com.woniu.bh.beautysalon.model.dto.CouponsDto;
import com.woniu.bh.beautysalon.model.params.CouponsParam;
import com.woniu.bh.beautysalon.web.service.IBeautysalonCouponService;
import com.woniu.bh.common.core.dto.ResultDto;
import com.woniu.bh.common.core.exception.ProjectException;
import com.woniu.bh.common.core.exception.ProjectExceptionResult;
import com.woniu.bh.common.core.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author daniel
 * @description: 优惠卷相关
 * @since 2020-10-16
 */
@RestController
@RequestMapping("/coupon")
@Slf4j
@Api(value = "美容馆接口", tags = "美容馆优惠券管理")
public class BeautysalonCouponController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private IBeautysalonCouponService bcs;

    /**
     * @description: 获取我所有的优惠券
     * @param:
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @GetMapping("/getAllCoupons")
    @ApiOperation("查询我的优惠券")
    public ResultDto getCoupons(HttpServletRequest request) {
        try {
            //解析token获取id
            String token = request.getHeader("X-Token");
            Integer beautysalonId = Integer.parseInt(JwtUtil.parseToken(token).get("beautysalonId").toString());
            List<CouponsDto> coupons = bcs.getAllCoupons(beautysalonId);
            return ResultDto.builder().success(false).code(1).message("操作成功").object(coupons).build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }
    /**
     * @description: 发送优惠券
     * @param: couponsParam
     * @return: com.woniu.bh.common.core.dto.ResultDto
     */
    @PostMapping("/distributeCoupons")
    @ApiOperation("发放优惠券")
    public ResultDto bsDistributeCoupons(CouponsParam couponsParam, HttpServletRequest request) {
        log.info("传入参数:{}", couponsParam);
        try {
            int result = bcs.distributeCoupons(couponsParam);
            if (result > 0) {
                return ResultDto.builder().success(true).code(1).message("操作成功").object(result).build();
            }
            return ResultDto.builder().success(false).code(22011)
                    .message("优惠券数量不足").build();
        } catch (ProjectException p) {
            p.printStackTrace();
            return ResultDto.builder().success(false).code(ProjectExceptionResult.SYSTEM_ERROR.getCode())
                    .message(ProjectExceptionResult.SYSTEM_ERROR.getMessage()).build();
        }
    }
}

